package com.sdcx.agrobank.util;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sdcx.agrobank.R;

/*
 *@Author:田恒宇
 *@Date: 2020-03-18
 *@Time:13:29
 *@Description:
 */
public class MyTextview extends LinearLayout {
    private TextView textView = null;
    protected int textColor;
    protected float textSize;
    protected int maxLine;
    protected String text;
    public int defaultTextColor = getResources().getColor(R.color.color_9);
    public int defaultTextSize = 14;
    public int defaultLine = 2;
    public Context context;

    public MyTextview(Context context) {
        super(context);
        initView();
    }

    public MyTextview(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        initAttrs(context, attrs);
        // textListener();
    }

    public MyTextview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initAttrs(context, attrs);
        //  textListener();
    }

    public void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextStyle);
        textColor = typedArray.getColor(R.styleable.MyTextStyle_textColor, defaultTextColor);
        textSize = typedArray.getDimensionPixelSize(R.styleable.MyTextStyle_textSize, defaultTextSize);
        maxLine = typedArray.getInt(R.styleable.MyTextStyle_maxLine, defaultLine);
        text = typedArray.getString(R.styleable.MyTextStyle_text);
        setMyView(textColor, textSize, maxLine, text);
        //用完 回收一下
        typedArray.recycle();
    }

    public void initView() {
     //   setOrientation(VERTICAL);
     //   setGravity(Gravity.LEFT);
        textView = new TextView(getContext());
        textView.setIncludeFontPadding(false);
        textView.setEllipsize(TextUtils.TruncateAt.END);
        textView.setLines(2);
        //替换textview字体
        textView.setTypeface(App.asset);
        addView(textView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }

    protected void setMyView(int color, float size, final int line, String text) {
        //文本设置颜色
        textView.setTextColor(color);
        //字体大小
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        //设置文字
        textView.setText(text);
        //设置TextView的高度
        textView.setHeight(textView.getLineHeight() * line);
    }

    protected void textListener() {
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // textAnimator();
            }
        });
    }

    boolean isGo;

    public void textAnimator(ImageView img) {
        isGo = !isGo;
        textView.clearAnimation();
        //相差的高度
        final int deltaValue;
        //初始的高度
        final int startValue = textView.getHeight();
        //动画播放的时间
        int duration = 200;
        if (isGo) {
            //textView.setLines(Integer.MAX_VALUE);
            //Image图片打开的动画
            deltaValue = textView.getLineHeight() * textView.getLineCount() - startValue;
            RotateAnimation animation = new RotateAnimation(0, 90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            animation.setDuration(duration);
            animation.setFillAfter(true);
            img.startAnimation(animation);
        } else {
            //Image图片关闭的动画
            deltaValue = textView.getLineHeight() * maxLine - startValue;
            RotateAnimation animation = new RotateAnimation(90, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            animation.setDuration(duration);
            animation.setFillAfter(true);
            img.startAnimation(animation);
        }
        //打开或者关闭的时候textview下面的展开动画
        Animation animation = new Animation() {
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                textView.setHeight((int) (startValue + deltaValue * interpolatedTime));
            }
        };
        animation.setInterpolator(new AccelerateInterpolator());
        animation.setDuration(duration);
        textView.startAnimation(animation);
    }

    public TextView getTextView() {
        return textView;
    }

    public void setText(CharSequence charSequence) {
        textView.setText(charSequence);
    }
}